import 'package:flutter/material.dart';
import 'package:flutter_pagewise/flutter_pagewise.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/routes/routes.dart';
import 'package:loxen/utils/config.dart';
import 'package:loxen/utils/image_url.dart';
import 'package:loxen/widget/my_subassembly.dart';
import 'package:provider/provider.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:loxen/viewmode/material_category_viewmodel.dart';

class MaterialListWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
      onRefresh: () async =>
          Provider.of<MaterialCategoryViewModel>(context, listen: false).refresh(),
      child: PagewiseListView<ItemEntity>(
        pageLoadController:
            Provider.of<MaterialCategoryViewModel>(context, listen: false).controller,
        loadingBuilder: (_) => MySubassembly.getLoadingItem(),
        noItemsFoundBuilder: (_) => MySubassembly.getNullItem(msg: '无更多素材'),
        itemBuilder: (context, item, index) {
          return Padding(
            padding: index == 0 ? EdgeInsets.zero : EdgeInsets.only(top: 20.h),
            child: Row(
              children: [
                InkWell(
                  onTap: () {
                    Navigator.of(context)
                        .pushNamed(Routes.material_detail, arguments: item.materialItem1.id);
                  },
                  child: CachedNetworkImage(
                    imageUrl: ImageUrlUtil.getClean(item.materialItem1.cover, w: 300),
                    fit: BoxFit.cover,
                    width: 330.w,
                    height: 330.w,
                  ),
                ),
                Expanded(child: Constants.empty),
                item.materialItem2 == null
                    ? Constants.empty
                    : InkWell(
                        onTap: () {
                          Navigator.of(context)
                              .pushNamed(Routes.material_detail, arguments: item.materialItem2.id);
                        },
                        child: CachedNetworkImage(
                          imageUrl: ImageUrlUtil.getClean(item.materialItem2.cover, w: 300),
                          fit: BoxFit.cover,
                          width: 330.w,
                          height: 330.w,
                        ),
                      )
              ],
            ),
          );
        },
      ),
    );

    // return RefreshIndicator(
    //   onRefresh: () async {
    //     Provider.of<MaterialCategoryViewModel>(context, listen: false).refresh();
    //   },
    //   child: PagewiseGridView<MaterialItem>.count(
    //     addAutomaticKeepAlives: false,
    //     showRetry: false,
    //     pageLoadController:
    //     Provider.of<MaterialCategoryViewModel>(context, listen: false).controller,
    //     errorBuilder: (_, __) {
    //       return Text('没有更多素材');
    //     },
    //     crossAxisSpacing: 20.w,
    //     mainAxisSpacing: 20.w,
    //     childAspectRatio: 1,
    //     crossAxisCount: 2,
    //     itemBuilder: (context, entity, index) {
    //       return InkWell(
    //         onTap: () {
    //           Navigator.of(context).pushNamed(Routes.material_detail, arguments: entity.id);
    //         },
    //         child: CachedNetworkImage(
    //           imageUrl: ImageUrlUtil.getClean(entity.cover, 300, 80),
    //           fit: BoxFit.cover,
    //           width: double.infinity,
    //           height: double.infinity,
    //         ),
    //       );
    //     },
    //   ),
    // );
  }
}
